package server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @desc 在使用readLine等一行方法时，需要注意\n字符
 * @author hongxingfan
 * @since 2015年2月5日 上午10:58:11
 * @major TODO
 */
public class Server1 {

    public static void listen() {
        try {
            ServerSocket server = new ServerSocket(8888);
            Socket socket = server.accept();
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            BufferedWriter os = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

            String line = null;
            line = in.readLine();
            while (true) {
                if (line != null) {
                    os.write(line + "\n");
                    os.flush();
                    System.out.println("Server Print: " + line + " :From Client!");
                } else {
                    break;
                }
            }
            // os.close();
            // in.close();
            // socket.close();
            // server.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        listen();
    }

}
